package com.yycx.common.mybatis.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.utils.StringUtils;
import lombok.Data;
import org.apache.commons.collections.MapUtils;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 分页请求参数
 *
 * @author zyf
 * @date 2018/07/10
 */
@Data
public class PageParams extends Page implements Serializable {
    private static final long serialVersionUID = -1710273706052960025L;
    /**
     * 查询主键
     */
    private Long id;
    /**
     * 当前页
     */
    private int page = CommonConstants.DEFAULT_PAGE;
    /**
     * 每页显示条数
     */
    private int limit = CommonConstants.DEFAULT_LIMIT;
    /**
     * 排序方式
     */
    private String sort = "desc";

    /**
     * 排序字段
     */
    private String order;
    /**
     * 接口扩展handler名称
     */
    private String handlerName;
    /**
     * 表单参数
     */
    private ConcurrentHashMap<String, Object> requestMap = new ConcurrentHashMap<>();

    /**
     * 查询开始日期
     */
    private String beginDate;

    /**
     * 查询结束日期
     */
    private String endDate;

    /**
     * 数据结果集是否展示分页总条数等数据
     */
    private Integer simpleData = 1;

    /**
     * 排序
     */
    private String orderBy;


    public PageParams() {
        requestMap = new ConcurrentHashMap<>();
    }

    public PageParams(Map map, String pageKey, String limitKey) {
        init(map, pageKey, limitKey);
    }

    public PageParams(Map map) {
        init(map, CommonConstants.PAGE_KEY, CommonConstants.PAGE_LIMIT_KEY);
    }

    public void init(Map map, String pageKey, String limitKey) {
        if (FlymeUtils.isNotEmpty(map)) {
            this.page = MapUtils.getInteger(map, pageKey, CommonConstants.DEFAULT_PAGE);
            this.limit = MapUtils.getInteger(map, limitKey, CommonConstants.DEFAULT_LIMIT);
            this.sort = MapUtils.getString(map, CommonConstants.PAGE_SORT_KEY);
            this.simpleData = MapUtils.getInteger(map, CommonConstants.SIMPLEDATA_KEY, 1);
            this.id = MapUtils.getLong(map, CommonConstants.PARAMS_ID_KEY);
            this.handlerName = MapUtils.getString(map, CommonConstants.HANDLER_NAME);
            this.order = MapUtils.getString(map, CommonConstants.PAGE_ORDER_KEY);
            this.beginDate = MapUtils.getString(map, CommonConstants.QUERY_BEGINDATE);
            this.endDate = MapUtils.getString(map, CommonConstants.QUERY_ENDDATE);
            this.setCurrent(page);
            this.setSize(limit);
            String searchCount = map.getOrDefault(CommonConstants.SEARCH_COUNT,"1").toString();
            if(searchCount.equals("false")||"0".equals(searchCount)){
                this.searchCount = false;
            }
            //删除map中的分页相关参数
            map.remove(CommonConstants.PAGE_KEY);
            map.remove(CommonConstants.PAGE_LIMIT_KEY);
            map.remove(CommonConstants.PAGE_SORT_KEY);
            map.remove(CommonConstants.PAGE_ORDER_KEY);
            map.remove(CommonConstants.HANDLER_NAME);
            map.remove(CommonConstants.TIMESTAMP);
            this.requestMap.putAll(map);
        }
    }


    public PageParams(int page, int limit) {
        this(page, limit, "", "");
    }

    public PageParams(int page, int limit, String sort, String order) {
        this.page = page;
        this.limit = limit;
        this.sort = sort;
        this.order = order;
    }

    public synchronized int getPage() {
        if (page <= CommonConstants.MIN_PAGE) {
            page = 1;
        }
        return page;
    }

    public synchronized int getLimit() {
        if (limit > CommonConstants.MAX_LIMIT) {
            limit = CommonConstants.MAX_LIMIT;
        }
        return limit;
    }

    public long getOffset() {
        return (this.getCurrent() - 1) * (long) getLimit();
    }


    /**
     * 把参数转换为实体类
     *
     * @param t
     * @param <T>
     * @return
     */
    public <T> T mapToObject(Class<T> t) {
        return BeanUtil.mapToBean(this.requestMap, t, false);
    }
}
